from rest_framework import serializers
from django.contrib.auth.models import User
from .models import UserRole

class UserRoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserRole
        fields = ('role', 'jnumber', 'img', 'phone', 'gender')

class UserSerializer(serializers.ModelSerializer):
    userrole = UserRoleSerializer()  # 嵌套包含 UserRoleSerializer
    formatted_date_joined = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'formatted_date_joined', 'userrole')

    def get_formatted_date_joined(self, obj):
        return obj.date_joined.strftime('%Y-%m-%d %H:%M:%S')
